<!-- 全局通用的认证Dialog组件, 默认Element Message实现 -->
<template>
  <div>
    <el-dialog
      title=""
      :visible.sync="visible"
      width="500px"
      :close-on-click-modal="false"
      :destroy-on-close="true"
      v-el-drag-dialog
      class="login-container2"
    >
      <!-- :show-close="false" -->
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
        <div class="title-container">
          <h3 class="title">Gazzele</h3>
          <h5 class="sub-title">Lightweight is not a fast development platform</h5>
        </div>

        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="Username"
            name="username"
            autocomplete="on"
          />
        </el-form-item>

        <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="Password"
              name="password"
              autocomplete="on"
              @keyup.native="checkCapslock"
              @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
        </el-tooltip>

        <el-button plain :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">Login</el-button>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import store from '@/store'

export default {
  data () {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      visible: true,
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      // showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  mounted () {
  },
  methods: {
    checkCapslock (e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd () {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          // 直接加密,会双向绑定登陆表单的密码,这里最好重新创建一个对象提交
          const loginUser = {}
          loginUser.password = this.loginForm.password
          loginUser.username = this.loginForm.username
          // loginUser.username = this.loginForm.username.toLowerCase() 处理大写问题
          store.dispatch('account/login', loginUser)
            .then(() => {
              // 关闭Dialog
              this.closeDialog()
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 关闭close
    closeDialog () {
      // 关闭Dialog
      this.visible = false

      // 销毁
      this.destroyDialog()
    },
    destroyDialog () {
      // 通过父级移除子元素的方式来移除该组件实例和 DOM 节点
      this.$destroy(true)
      this.$el.parentNode.removeChild(this.$el)
    }
  }
}

</script>

<style lang="scss" scoped>
.login-container2 {
  width: 100%;
  overflow: hidden;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    background: #ffffff;

    >>> input {
      background: transparent;
      // border: 3px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      height: 47px;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #fff inset !important;
        // -webkit-text-fill-color: #fff !important;
      }
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 48px;
      margin: 0px auto 10px auto;
      text-align: center;
      font-weight: bold;
    }

    .sub-title{
      font-size: 12px;
      margin: 0px auto 25px auto;
      text-align: center;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
